package com.chinaztt.fda.utils;

import org.apache.http.NameValuePair;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class SignUtil {
	
	private SignUtil() {};
	
	
	@SuppressWarnings("unchecked")
	public static String sign(ArrayList<NameValuePair> list){
		String MD5KEY = "md5key";
		String DESKEY = "deskeydeskey";
		;
		//��key��ֵ���ֵ���������
		Collections.sort(list, new SortByName());
		String signCode="";
		
		for (int i = 0; i < list.size(); i++) {  
            NameValuePair nowPair = list.get(i);  
            String value = nowPair.getValue();  
            try {  
               // value = URLEncoder.encode(value, "UTF-8");  
            } catch (Exception e) {  
                  
            }  
            signCode = signCode+nowPair.getName()+value;
        }  
		String afterDes = EncryptUtil.doDESEncode(signCode,DESKEY);
		String afterMD5 = ByteUtil.MD5(afterDes + MD5KEY);
		return afterMD5;
	}

}

@SuppressWarnings("rawtypes")
class SortByName implements Comparator {
	 @Override
	public int compare(Object o1, Object o2) {
		 NameValuePair s1 = (NameValuePair) o1;
		 NameValuePair s2 = (NameValuePair) o2;
	  if(s1.getName().compareTo(s2.getName()) < 0)
		  return -1;
	  else if (s1.getName().compareTo(s2.getName()) > 0) {
		  return 1;
	  }
	  
	  return 0;
	 }
}

